package com.redlion.object;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.adt.pool.GenericPool;
import org.andengine.util.debug.Debug;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.Game;

public class ExplosionPool extends GenericPool<AnimatedExplosion> {
	public ITiledTextureRegion mTextureRegion;
	public FixtureDef mFixtureDef;
	private Scene mScene;

	public ExplosionPool(ITiledTextureRegion tiledTextureRegion, FixtureDef fixtureDef, Scene scene) {
		if (tiledTextureRegion == null) {
			throw new IllegalArgumentException("The texture region must not be NULL");
		}
		if (fixtureDef == null) {
			throw new IllegalArgumentException("The fixture definition must not be NULL");
		}
		mTextureRegion = tiledTextureRegion;
		mFixtureDef = fixtureDef;
		mScene = scene;
		batchAllocatePoolItems(20);
	}

	/**
	 * Called when a Bullet is required but there isn't one in the pool
	 */
	@Override
	protected AnimatedExplosion onAllocatePoolItem() {
		Debug.d("ExplosionPool", "onAllocatePoolItem");
		AnimatedExplosion explosion = new AnimatedExplosion(Vector2Pool.obtain(), mTextureRegion, mFixtureDef);
		mScene.attachChild(explosion.sprite);
		explosion.deactivate();
		return explosion;
	}

	/**
	 * Called when a Bullet is sent to the pool
	 */
	@Override
	protected void onHandleRecycleItem(final AnimatedExplosion explosion) {
		Debug.d("ExplosionPool", "onHandleRecycleItem");
		explosion.deactivate();
	}

	/**
	 * Called just before a Bullet is returned to the caller, this is where you
	 * write your initialize code i.e. set location, rotation, etc.
	 */
	@Override
	protected void onHandleObtainItem(final AnimatedExplosion explosion) {
		Debug.d("ExplosionPool", "onHandleObtainItem");
		// final Vector2 temp =
		// Vector2Pool.obtain(mCannon.getShotOrigin(false)).mul(1f / 32f);
		// explosion.body.setTransform(temp.x, temp.y, 0);
		// Vector2Pool.recycle(temp);
		explosion.activate();
	}
}
